Chapter 9: Exercises

Part 1

  1. Matrix determinant and product of eigenvalues: 在數學上,我們可以證明:一個方陣的行列式,會等於它的 eigenvalue 的乘積。試寫一個 MATLAB 程式,任意產生不同維度的 10 個方陣,來驗證上述恆等式。
  2. tr(A) 定義為方陣 A 的主對角線元素的和,在數學上,只要矩陣 A 和矩陣 B 的乘積是一個方陣,我們可以證明 tr(AB)=tr(BA)。試寫一個 MATLAB 程式,任意產生不同維度的 10 組 A、B,以驗證上述恆等式。
  3. 我們已經知道 sort 指令的用法是 [sortedVec, index] = sort(vec),請寫一個函數 invSort,是 sort 的反運算,產生的效果是 vec = invSort(sortedVec, index)。
    提示:你可以使用 sort 指令來快速地達到此功能。
  4. 請設計一個函數 index2pos.m,其用法如下:
    position = index2pos(index);
    其中 index 是一個元素為正整數的向量,position 則是此函數的輸出矩陣,其列數為 index 元素的最大值,行數則是 index 的元素個數,而且當 index(i) = j 時,position 的第 i 行中,只有第 j 個元素為 1,其餘為零。例如,當 index 是 [1 3 5 2] 時,傳回的 position 是 $$ \left[ \begin{matrix} 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0\\ \end{matrix} \right] $$
  5. 請設計一個函數 copyMatrix.m,其用法如下:
    bigMatrix = copyMatrix(matrix, p, q);
    其中 matrix 是一個輸入矩陣,p 和 q 是正整數,函數 copyMatrix 的作用是將 matrix 視為一個元素,並將之複製成 p x q 的大矩陣 bigMatrix,其列數為 p*size(matrix,1),行數為 q*size(matrix,2)。例如,當 a = [1 2; 3 4] 時,copyMatrix(a, 2, 5) 所產生的輸出矩陣是: $$ \left[ \begin{matrix} 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2\\ 3 & 4 & 3 & 4 & 3 & 4 & 3 & 4 & 3 & 4\\ 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2\\ 3 & 4 & 3 & 4 & 3 & 4 & 3 & 4 & 3 & 4\\ \end{matrix} \right] $$
  6. 設計一個函數 copyElement.m,其用法如下:
    bigMatrix = copyElement(matrix, p, q);
    其中 matrix 是一個輸入矩陣,p 和 q 是正整數,函數 copyElement 的作用是將 matrix 的每一個元素複製成 p x q 的矩陣後,再合成一個大矩陣 bigMatrix,其列數為 p*size(matrix,1),行數為 q*size(matrix,2)。例如,當 a = [1 2; 3 4] 時,copyElement(a, 2, 5) 所產生的輸出矩陣是: $$ \left[ \begin{matrix} 1 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 2 & 2\\ 1 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 2 & 2\\ 3 & 3 & 3 & 3 & 3 & 4 & 4 & 4 & 4 & 4\\ 3 & 3 & 3 & 3 & 3 & 4 & 4 & 4 & 4 & 4\\ \end{matrix} \right] $$
  7. 執行下列程式碼後,請問 size(X) 和 size(y) 各是多少? x = rand(10,8,16); y = permute(x, [2 3 1]);
  8. Give an expression that multiplies two vectors to obtain the matrix [1 2 3 4 5;1 2 3 4 5;1 2 3 4 5].
    Ans: [1 1 1]'*[1 2 3 4 5]

Part 2

  1. Extract nonzero elements in a vector: How do you write a single MATLAB statement to extract nonzero elements of a vector A and assign them to A2?
    Ans: A2=A(A~=0); or A2=A(find(A));
  2. Matrix indexing: Matrix A is expressed as $$ A= \left[ \begin{matrix} 4 & 10 & 1 & 6 & 2\\ 8 & 2 & 9 & 4 & 7\\ 7 & 5 & 7 & 1 & 5\\ 0 & 3 & 4 & 5 & 4\\ 23 & 13 & 13 & 0 & 3\\ \end{matrix} \right] $$ Which are the correct ways to assign the last column to matrix B?
    1. B=A(1:5, 5);
    2. B=A(1:end, end);
    3. B=A(:, 5);
    4. B=A(:, end);
    5. B=A(21:25);
    6. B=A(21:end);
  3. More on matrix indexing: Given a 2-dimensional matrix A, write a one-line MATLAB statement (as simple as possible) to achieve the following goals:
    1. Assign the first column of matrix A to variable B.
    2. Assign the last column of matrix A to variable B.
    3. Assign the second column from the end of matrix A to variable B.
    4. Assign the second row from the end of matrix A to variable B.
    5. Assign the maximum value of matrix A to variable B.
  4. About 'diag' command: Suppose that A = [1 2 3; 4 5 6; 7 8 9].
    1. What is returned by diag(A)?
    2. What is returned by diag(diag(A))?
  5. Using 'diag' command: Given a matrix A, you want to divided each column by its column sum, and assign the column-normalized version to variable B.
    1. Write a one-line MATLAB statement to achieve the goal, which involves the use of 'diag' command.
    2. Write a one-line MATLAB statement to achieve the goal, which involves the use of 'bsxfun' command.
  6. About 'hilb' command: Plot log(det(hilb(n))) versus n when n is varied from 1 to 40. Which value of n will make det(hilb(n)) zero?
  7. About transpose: Suppose that A = [1+i, 2; 3, 1+2i].
    1. What is A'?
    2. What is A.'?
  8. Lp-norm:
    1. Give the definition of Lp-norm for a vector $\mathbf{x}=[x_1, x_2, ..., x_n]$.
    2. Let $L_p(x)$ be the Lp-norm of a vector $x$. Prove that $L_1(\mathbf{x}) \geq L_2(\mathbf{x}) \geq L_\infty(\mathbf{x})$.
    3. Derive the explicit analytic form of $L_\infty(\mathbf{x})$.
    4. In a 2D space, plot the following sets of $\mathbf{x}=[x_1, x_2]$:
      1. $\{\mathbf{x}|L_1(\mathbf{x})=1\}$
      2. $\{\mathbf{x}|L_2(\mathbf{x})=1\}$
      3. $\{\mathbf{x}|L_\infty(\mathbf{x})=1\}$
  9. Sorting and its inverse: For a given vector x, we can sort its element using the "sort" command:
    [x2, index]=sort(x);
    the sorted vector x2 is actually equal to x(index). Write a function "sortInv" that returns x from given x2 and index, that is:
    x=sortInv(x2, index);
    To test your function, try the following script and the value of z should be 1: x=round(100*rand(1,100)); [x2, index]=sort(x); x3=sortInv(x2, index); z=isequal(x, x3) (Hint: You can use "sort" command to create the function, which can be as short as two major lines of statements in the function body.)
  10. Various integer data types: How many bytes are used for each of the following data types? And what is the corresponding numerical range of the data type?
    1. uint8
    2. uint16
    3. uint32
    4. int8
    5. int16
    6. int32
  11. Other data types: How many bytes are used for each of the following data types?
    1. single
    2. double
    3. char
  12. Operations on uint8: Suppose that v=uint8([4, 9, 2]). Give the values of the following expressions in MATLAB.
    1. 2.5*v
    2. 2.4*v
    3. 50*v
    4. -50*v
  13. Operations on int8: Suppose that v=int8([4, 9, 2]). Give the values of the following expressions in MATLAB.
    1. 2.5*v
    2. 2.4*v
    3. 50*v
    4. -50*v
  14. 請參考下列程式碼: n = 10000; vec = rand(n, 1); ratio = sum(vec>0.7)/n; 當 n 的值越來越大時,ratio 的值會逼近於多少?為什麼?

MATLAB程式設計:入門篇